package com.cmxh.cloundnote.Controller;



import com.cmxh.cloundnote.pojo.Category;
import com.cmxh.cloundnote.service.CategoryService;
import com.cmxh.cloundnote.util.Code;
import com.cmxh.cloundnote.util.PageUtil;
import com.cmxh.cloundnote.util.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @RequestMapping("/findAll")
    public Result findAll(@RequestBody PageUtil pageUtil){
       // 返回的PageInfo包含：当前页数据列表、总页数、当前页码、每页数量等分页信息
        PageInfo<Category> list = categoryService.findAll(pageUtil);
        /**后端控制台调试**/
        System.out.println(list);
        if (list != null){
            return new Result(Code.GET_OK,list,"查询成功");
        }else {
            return new Result(Code.GET_ERR,"查询失败");
        }
    }
    @RequestMapping("/add")
    public Result addCategory(@RequestBody Category category){
        System.out.println(category);
        int i = categoryService.addCategory(category);
        if (i > 0){
            return new Result(Code.UPDATE_OK,"添加成功");
        }else {
            return new Result(Code.UPDATE_ERR,"添加失败");
        }

    }
    @RequestMapping("/deleted/{id}")
    public Result deleted(@PathVariable Integer id){
        int i = categoryService.deleted(id);
        if (i > 0){
            return new Result(Code.UPDATE_OK,"删除成功");
        }else {
            return new Result(Code.UPDATE_ERR,"删除失败");
        }
    }
    /**修改**/
    @RequestMapping("/update")
    public Result updateCategory(@RequestBody Category category){
        System.out.println(category);
        int i = categoryService.updateCategory(category);
        if (i > 0){
            return new Result(Code.UPDATE_OK,"修改成功");
        }else {
            return new Result(Code.UPDATE_ERR,"修改失败");
        }
    }
    @RequestMapping("/add/{uid}")
    public Result add(@PathVariable("uid") Integer id){
        List<Category> byId = categoryService.findById2(id);
        if (byId!=null){
            return new Result(Code.GET_OK,byId,"查询成功");
        }else {
            return new Result(Code.GET_ERR,"查询失败");
        }
    }

}
